/*
*  Copyright (C) 2009  Peter Kist & Jan Ripke
*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __mediator_detail_mediator_impl_h
#define __mediator_detail_mediator_impl_h 1

#include <vector>
#include <boost/unordered_map.hpp>

#include <Core/Mediator/Event.h>

namespace galaxy {
    namespace mediator {

        class Identifier;
        class Consumer;
        class Producer;

        namespace detail {

            /** 
            * This class enables communication between modules, (sub) systems and components through a
            * uniform interface. The mediator is a broker between producers that sends events to 
            * consumers that are interested on the one hand.
            * On the other hand, a consumer can Request a producer for required information.
            */
            class MediatorImpl
            {
                typedef boost::unordered_map<int, Producer*>         ProducerMap;
                typedef boost::unordered_map<std::string, Consumer*> ConsumerMap;
                typedef std::vector<Consumer*>                       ConsumerList;
                typedef boost::unordered_map<int, ConsumerList*>     InterestMap;

                /** consumers, registered by consumerType */
                ProducerMap m_Producers;
                ConsumerMap m_Consumers;
                InterestMap m_InterestedConsumers;

            public:
                MediatorImpl(void);
                ~MediatorImpl(void);

                virtual bool registerProducer(Producer& producer);
                virtual bool unregisterProducer (int category);

                virtual bool registerConsumer (Consumer& consumer);
                virtual bool unregisterConsumer (std::string& consumerType);

                virtual bool registerInterest (std::string& consumerType, Identifier& topic);

                virtual bool postEvent(Event& event, PostMethod postMethod);
                virtual bool RequestProducer(const Request &Request, RequestResultPtr& result);

            private:
                void notifyConsumers (ConsumerList& consumerList, Event& event, PostMethod postMethod);
            };

        } // detail
    } // mediator
} // galaxy

#endif // __mediator_detail_mediator_impl_h
